<?php
namespace app\api\controller;
use app\common\controller\CommonApi;

/**
 * 关注、粉丝
 */
class Care extends CommonApi{
    protected $tn = 'user_care';


    //添加关注
    public function addCare(){
        if(!isset($this->post['user_id']) || intval($this->post['user_id']) <= 0){
            $this->apiReturn(0,'参数user_id丢失');
        }

        if(intval($this->post['user_id']) == UID){
            $this->apiReturn(0,'不能关注自己');
        }

        if(db($this->tn)->where('care_id',intval($this->post['user_id']))->where('user_id',UID)->find()){
            $this->apiReturn(1,'关注成功');
        }

        db()->startTrans();
        if(!db($this->tn)->insert(
            [
                'user_id' => UID,
                'care_id' => intval($this->post['user_id']),
                'add_time' => time()
            ])){
            db()->rollback();
            $this->apiReturn(0,'关注失败，请稍后再试');
        }

        //添加关注量
        if(!db('user')->where('id',UID)->setInc('care_num')){
            db()->rollback();
            $this->apiReturn(0,'添加关注量失败');
        }
        //添加粉丝量
        if(!db('user')->where('id',intval($this->post['user_id']))->setInc('fans_num')){
            db()->rollback();
            $this->apiReturn(0,'添加粉丝量失败');
        }
        db()->commit();
        $this->apiReturn(1,'关注成功');
    }


    /**
     * [offCare 取消关注]
     * @return [type] [description]
     */
    public function offCare(){
        if(!isset($this->post['user_id']) || intval($this->post['user_id']) <= 0){
            $this->apiReturn(0,'参数user_id丢失');
        }

        if(!db($this->tn)->where('care_id',intval($this->post['user_id']))->where('user_id',UID)->find()){
            $this->apiReturn(1,'取消关注成功');
        }

        db()->startTrans();
        if(!db($this->tn)->where('care_id',intval($this->post['user_id']))->where('user_id',UID)->delete()){
            db()->rollback();
            $this->apiReturn(0,'取消关注失败,请稍后再试');
        }
        if(!db('user')->where('id',UID)->setDec('care_num')){
            db()->rollback();
            $this->apiReturn(0,'操作失败，请稍后再试');
        }
       
        if(!db('user')->where('id',intval($this->post['user_id']))->setDec('fans_num')){
            db()->rollback();
            $this->apiReturn(0,'操作失败，请稍后再试1');
        }
        db()->commit();
        $this->apiReturn(1,'取消关注成功');
    }


    //我的粉丝
    public function myFans(){
        $list = db($this->tn)->alias('c')->join(['bz_user' => 'u'],'u.id = c.user_id')
            ->where('c.care_id',UID)
            ->field('u.id as user_id,nick_name,photo_path,sex,zhiye,kaopu,service_count,sign')
            ->limit(($this->p-1)*config('page_size'),config('page_size'))
            ->order('c.add_time desc')
            ->select();
        foreach($list as $key => $val){
            $list[$key]['zhiye'] = $val['zhiye'] == 0 ? "" : db('zhiye')->where('id',$val['zhiye'])->value('name');
        }
        $this->apiReturn(1,'列表',$list);
    }

    //我的关注
    public function myCare(){
        $list = db($this->tn)->alias('c')->join(['bz_user' => 'u'],'u.id = c.care_id')
            ->where('c.user_id',UID)
            ->field('u.id as user_id,nick_name,photo_path,sex,zhiye,kaopu,service_count,sign')
            ->limit(($this->p-1)*config('page_size'),config('page_size'))
            ->order('c.add_time desc')
            ->select();
        foreach($list as $key => $val){
            $list[$key]['zhiye'] = $val['zhiye'] == 0 ? "" : db('zhiye')->where('id',$val['zhiye'])->value('name');
        }
        $this->apiReturn(1,'列表',$list);
    }
}
